/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.security.configuration;

import java.util.Arrays;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.iiifi.kite.configuration.KiteProperties;
import com.iiifi.kite.security.handle.KitePermissionsFun;
import com.iiifi.kite.security.interceptor.KiteSecurityInterceptor;
import com.iiifi.kite.security.model.KiteRestTemplate;
import com.iiifi.kite.security.properties.KiteSecurityProperties;
import com.iiifi.kite.security.service.KiteSecurityService;
import com.iiifi.kite.security.service.impl.KiteSecurityServiceDefaultImpl;

import lombok.AllArgsConstructor;

/**
 * 配置类
 *
 * @author kite@iiifi.com 花朝
 */
@Configuration
@AllArgsConstructor
@AutoConfigureAfter(KiteSecurityProperties.class)
@EnableConfigurationProperties(KiteSecurityProperties.class)
@ConditionalOnProperty(value = "kite.security.enabled", havingValue = "true", matchIfMissing = true)
public class KiteSecurityAutoConfiguration implements WebMvcConfigurer {

    private final Environment environment;
    private final KiteProperties kiteProperties;
    private final KiteRestTemplate kiteRestTemplate;
    private final KiteSecurityProperties kiteSecurityProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 增加swagger放行url
        String[] split = kiteSecurityProperties.getExcludeSwaggerUrl().split(",");
        kiteSecurityProperties.getExcludePatterns().addAll(0, Arrays.asList(split));
        // sso拦截器
        registry.addInterceptor(new KiteSecurityInterceptor(environment, kiteProperties, kiteSecurityService()))
                .addPathPatterns(kiteSecurityProperties.getIncludePatterns().toArray(new String[0]))
                .excludePathPatterns(kiteSecurityProperties.getExcludePatterns().toArray(new String[0]));
    }

    /**
     * 提供给 模板引擎使用
     *
     * <pre>
     * th:if="${@kite.hasPermission('admin:edit:pwd')}"
     * </pre>
     *
     * @return KitePermissionsFun
     */
    @Bean("kite")
    public KitePermissionsFun kitePermissionsFun() {
        return new KitePermissionsFun();
    }

    @Bean
    @ConditionalOnMissingBean(value = KiteSecurityService.class)
    public KiteSecurityService kiteSecurityService() {
        return new KiteSecurityServiceDefaultImpl(kiteSecurityProperties, kiteRestTemplate);
    }
}
